package Com.Isman.WebService;


import java.util.ArrayList;
import java.util.List;
import java.util.Vector;

import org.kobjects.base64.Base64;
import org.ksoap2.SoapEnvelope;
import org.ksoap2.serialization.SoapObject;
import org.ksoap2.serialization.SoapSerializationEnvelope;
import org.ksoap2.transport.AndroidHttpTransport;

import Com.Isman.WebService.ComplexType.Demo;
import Com.Isman.WebService.ComplexType.Hotel;
import Com.Isman.WebService.ComplexType.HotelDetail;
import Com.Isman.WebService.ComplexType.Image;
import Com.Isman.WebService.ComplexType.Order;
import Com.Isman.WebService.ComplexType.Room;
import android.util.Log;

@SuppressWarnings("deprecation")
public class ServiceConnect{

	private static final String NAMESPACE = "http://tempuri.org";
	private static final String URL = "http://192.168.137.1/hotel/Service/BookingService.php?wsdl";
    
    public boolean debugMode = false;
    private String requestString;
    private String responseString;
    
    

	public boolean callCancel(int orderId){
		return true;
	}
	public boolean callCreateOrder(int hotelId,int roomId,String deviceId,
			int roomCount,double amount,double discount,String checkInDate,String checkOutDate,String notation){
		String METHOD_NAME = "GetGalleryImage";
		
		//Create request
		SoapObject requestObject = new SoapObject(NAMESPACE, METHOD_NAME);
		
		//Set request.
		requestObject.addProperty("hotelId", hotelId);
		requestObject.addProperty("roomId", roomId);
		requestObject.addProperty("deviceId", deviceId);
		requestObject.addProperty("roomCount", roomCount);
		requestObject.addProperty("amount", amount);
		requestObject.addProperty("discount",discount);
		requestObject.addProperty("checkInDate",checkInDate);
		requestObject.addProperty("checkOutDate",checkOutDate);
		requestObject.addProperty("notation",notation);
	
		
		//Create soapEnvelop
		SoapSerializationEnvelope soapEnvelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
		soapEnvelope.setOutputSoapObject(requestObject);
		
		
		//Create transport.
		AndroidHttpTransport transport = new AndroidHttpTransport(URL);
		transport.debug = debugMode;
		
		//response
		boolean response = false;
		
		try {
			//Call Method
			transport.call(NAMESPACE+"/"+METHOD_NAME, soapEnvelope);
			
			//Debug mode
			if(debugMode = true){
				responseString = transport.responseDump;
				requestString = transport.requestDump;
			}
			
			//Get response
			SoapObject soap 	= (SoapObject)soapEnvelope.getResponse();
			response = Boolean.parseBoolean(soap.getProperty("return").toString());
			
			Log.i("Isman","Call "+METHOD_NAME+" Successful");
		} catch (Exception e) {
			Log.e("Isman","Call "+METHOD_NAME+" "+e.toString());
		}
		return response;
	}
	
    public Demo callDemo(){
		String METHOD_NAME = "MyHello";
		
		final SoapObject resquest = new SoapObject(NAMESPACE, METHOD_NAME);

        
        final SoapSerializationEnvelope soapEnvelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
        soapEnvelope.setOutputSoapObject(resquest);
        

		AndroidHttpTransport transport = new AndroidHttpTransport(URL);
        transport.debug = debugMode;

        Demo response = null;
        try {
        	//Call MyHello method.
        	transport.call("http://tempuri.org/"+METHOD_NAME, soapEnvelope);
			if(debugMode = true){
				responseString = transport.responseDump;
				requestString = transport.requestDump;
			}
        	//Receive and convert Object.
        	response = new Demo((SoapObject)soapEnvelope.getResponse());

        } catch (Exception e) {
        	e.printStackTrace();
		}
        return response;
	}
	
	public Image callGetGalleryImage(int hotelId,String imageName) {
		String METHOD_NAME = "GetGalleryImage";
		
		//Create request
		SoapObject requestObject = new SoapObject(NAMESPACE, METHOD_NAME);
		
		//Set request.
		requestObject.addProperty("hotelId", hotelId);
		requestObject.addProperty("imageName",imageName);
		
		//Create soapEnvelop
		SoapSerializationEnvelope soapEnvelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
		soapEnvelope.setOutputSoapObject(requestObject);
		
		
		//Create transport.
		AndroidHttpTransport transport = new AndroidHttpTransport(URL);
		transport.debug = debugMode;
		
		//response
		Image response = null;
		
		try {
			//Call Method
			transport.call(NAMESPACE+"/"+METHOD_NAME, soapEnvelope);
			
			//Debug mode
			if(debugMode = true){
				responseString = transport.responseDump;
				requestString = transport.requestDump;
			}
			
			//Get response
			SoapObject soap 	= (SoapObject)soapEnvelope.getResponse();
			response = new Image(soap);
			
			Log.i("Isman","Call "+METHOD_NAME+" Successful");
		} catch (Exception e) {
			Log.e("Isman","Call "+METHOD_NAME+" "+e.toString());
		}
		return response;
	}
	
	public List<Image> callGetGalleryThump(int hotelId){
		String METHOD_NAME = "GetGalleryThump";
		
		//Create request
		SoapObject requestObject = new SoapObject(NAMESPACE, METHOD_NAME);
		
		//Set request.
		requestObject.addProperty("hotelId", hotelId);
		
		//Create soapEnvelop
		SoapSerializationEnvelope soapEnvelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
		soapEnvelope.setOutputSoapObject(requestObject);
		
		
		//Create transport.
		AndroidHttpTransport transport = new AndroidHttpTransport(URL);
		transport.debug = debugMode;
		
		//response
		List<Image> response = new ArrayList<Image>();
		
		try {
			//Call Method
			transport.call(NAMESPACE+"/"+METHOD_NAME, soapEnvelope);
			
			//Debug mode
			if(debugMode = true){
				responseString = transport.responseDump;
				requestString = transport.requestDump;
			}
			
			//Get response
			SoapObject soap 	= (SoapObject)soapEnvelope.bodyIn;
			Vector<?> vector 	= (Vector<?>)soap.getProperty("return");

			for (int i = 0; i < vector.size(); i++) {
				response.add(new Image((SoapObject)vector.elementAt(i)));
			}
			
			Log.i("Isman","Call "+METHOD_NAME+" Successful");
		} catch (Exception e) {
			Log.e("Isman","Call "+METHOD_NAME+" "+e.toString());
		}
		return response;
	}
	
	public HotelDetail callGetHotelDetail(int hotelId,String checkInDate,String checkOutDate){
		String METHOD_NAME = "GetHotelDetail";
		
		//Create request
		SoapObject requestObject = new SoapObject(NAMESPACE, METHOD_NAME);
		
		//Set request.
		requestObject.addProperty("hotelId", hotelId);
		requestObject.addProperty("checkInDate", checkInDate);
		requestObject.addProperty("checkOutDate", checkOutDate);
		
		//Create soapEnvelop
		SoapSerializationEnvelope soapEnvelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
		soapEnvelope.setOutputSoapObject(requestObject);
		
		
		//Create transport.
		AndroidHttpTransport transport = new AndroidHttpTransport(URL);
		transport.debug = debugMode;
		
		//response
		HotelDetail response = null;
		try {
			//Call Method
			transport.call(NAMESPACE+"/"+METHOD_NAME, soapEnvelope);
			
			//Debug mode
			if(debugMode = true){
				responseString = transport.responseDump;
				requestString = transport.requestDump;
			}
			
			//Get response
			SoapObject soap =  (SoapObject)soapEnvelope.getResponse();
			response = new HotelDetail(soap);
			
			Log.i("Isman","Call "+METHOD_NAME+" Successful");
		} catch (Exception e) {
			Log.e("Isman","Call "+METHOD_NAME+" "+e.toString());
		}
		return response;
	}
	
	public List<Hotel> callGetHotelList(String input, int startLimit, int endLimit){
		final String METHOD_NAME = "GetHotelList";
		
		//Encode data.
		input = Base64.encode(input.getBytes());
		
		//Create request
		final SoapObject requestObject = new SoapObject(NAMESPACE,METHOD_NAME);
		
		//Set request.
		requestObject.addProperty("inout", input);
		requestObject.addProperty("startLimit", startLimit);
		requestObject.addProperty("endLimit", endLimit);
		
		//Create soapEnvelop
		final SoapSerializationEnvelope soapEnvelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
		soapEnvelope.setOutputSoapObject(requestObject);
		
		
		//Create transport.
		final AndroidHttpTransport transport = new AndroidHttpTransport(URL);
		transport.debug = debugMode;
		
		//response
		List<Hotel> response = new ArrayList<Hotel>();

		try {
			//Call Method
			transport.call(NAMESPACE+"/"+METHOD_NAME, soapEnvelope);
			
			//Debug Mode
			if(debugMode = true){
				responseString = transport.responseDump;
				requestString = transport.requestDump;
			}
			
			//Get response
			SoapObject soapObject = (SoapObject)soapEnvelope.bodyIn;
			Vector<?> vector = (Vector<?>)soapObject.getProperty("return");

			for (int i = 0; i < vector.size(); i++) {
				response.add(new Hotel((SoapObject)vector.elementAt(i)));
			}
			
			Log.i("Isman","Call "+METHOD_NAME+" Successful");
		} catch (Exception e) {
			Log.e("Isman","Call "+METHOD_NAME+" "+e.toString());
		}
		
		return response;
	}

	public List<Hotel> callGetHotelListByLocation(String string, String string2, int startLimit, int endLimit) {
		final String METHOD_NAME = "GetHotelList";
		
		//Create request
		final SoapObject requestObject = new SoapObject(NAMESPACE,METHOD_NAME);
		
		//Set request.
		requestObject.addProperty("inout", "");
		requestObject.addProperty("startLimit", startLimit);
		requestObject.addProperty("endLimit", endLimit);
		
		//Create soapEnvelop
		final SoapSerializationEnvelope soapEnvelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
		soapEnvelope.setOutputSoapObject(requestObject);
		
		
		//Create transport.
		final AndroidHttpTransport transport = new AndroidHttpTransport(URL);
		transport.debug = debugMode;
		
		//response
		List<Hotel> response = new ArrayList<Hotel>();

		try {
			//Call Method
			transport.call(NAMESPACE+"/"+METHOD_NAME, soapEnvelope);
			
			//Debug Mode
			if(debugMode = true){
				responseString = transport.responseDump;
				requestString = transport.requestDump;
			}
			
			//Get response
			SoapObject soapObject = (SoapObject)soapEnvelope.bodyIn;
			Vector<?> vector = (Vector<?>)soapObject.getProperty("return");

			for (int i = 0; i < vector.size(); i++) {
				response.add(new Hotel((SoapObject)vector.elementAt(i)));
			}
			
			Log.i("Isman","Call "+METHOD_NAME+" Successful");
		} catch (Exception e) {
			Log.e("Isman","Call "+METHOD_NAME+" "+e.toString());
		}
		
		return response;
	}
	
	public List<Order> callGetOrderList(String deviceId,int startLimit, int endLimit){
		String METHOD_NAME = "GetOrderList";
		
		//Create request
		SoapObject requestObject = new SoapObject(NAMESPACE, METHOD_NAME);
		
		//Set request.
		requestObject.addProperty("deviceId", deviceId);
		requestObject.addAttribute("startLimit",startLimit);
		requestObject.addAttribute("endtLimit",endLimit);
		
		//Create soapEnvelop
		SoapSerializationEnvelope soapEnvelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
		soapEnvelope.setOutputSoapObject(requestObject);
		
		
		//Create transport.
		AndroidHttpTransport transport = new AndroidHttpTransport(URL);
		transport.debug = debugMode;
		
		//response
		List<Order> response = new ArrayList<Order>();
		
		try {
			//Call Method
			transport.call(NAMESPACE+"/"+METHOD_NAME, soapEnvelope);
			
			//Debug mode
			if(debugMode = true){
				responseString = transport.responseDump;
				requestString = transport.requestDump;
			}
			
			//Get response
			SoapObject soapObject 	= (SoapObject)soapEnvelope.bodyIn;
			Vector<?> vector = (Vector<?>)soapObject.getProperty("return");
			
			for(int i=0;i<vector.size();i++){
				response.add(new Order((SoapObject)vector.elementAt(i)));
			}
			
			Log.i("Isman","Call "+METHOD_NAME+" Successful");
		} catch (Exception e) {
			Log.e("Isman","Call "+METHOD_NAME+" "+e.toString());
		}
		return response;
	}

	public List<Room> callGetRoomList(int hotelId,String checkInDate,String checkOutDate){
		String METHOD_NAME = "GetRoomList";
		debugMode = true;
		//Create request
		SoapObject requestObject = new SoapObject(NAMESPACE, METHOD_NAME);
		
		//Set request.
		requestObject.addProperty("hotelId", hotelId);
		requestObject.addProperty("checkInDate", checkInDate);
		requestObject.addProperty("checkOutDate", checkOutDate);
		
		//Create soapEnvelop
		SoapSerializationEnvelope soapEnvelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
		soapEnvelope.setOutputSoapObject(requestObject);
		
		
		//Create transport.
		AndroidHttpTransport transport = new AndroidHttpTransport(URL);
		transport.debug = true;
		
		//response
		List<Room> response = new ArrayList<Room>();
		
		try {
			//Call Method
			transport.call(NAMESPACE+"/"+METHOD_NAME, soapEnvelope);

			//Debug Mode
			if(debugMode = true){
				responseString = transport.responseDump;
				requestString = transport.requestDump;
			}
			
			//Get response
			SoapObject soapObject = (SoapObject)soapEnvelope.bodyIn;
			Vector<?> vector = (Vector<?>)soapObject.getProperty("return");

			for (int i = 0; i < vector.size(); i++) {
				response.add(new Room((SoapObject)vector.elementAt(i)));
			}
			
			Log.i("Isman","Call "+METHOD_NAME+" Successful");
		} catch (Exception e) {
			Log.e("Isman","Call "+METHOD_NAME+" "+e.toString());
		}
		return response;
	}

	public String getRequestString() {
		return requestString;
	}

	public String getResponseString() {
		return responseString;
	}

}
